new Vue({
    el: '#app',
    data: {
        // 加载
        loading: false,
        loadingEditCategory: false,
        categories: [],
        categoryOpen: ['不公开', "公开"],
        // 正在修改分类
        editingCategory: false,
        // 当前正在修改的分类
        currentCategory: {},
        curPrefixUrl: prefixUrl + gatewayPrefix.question
    },
    methods: {
        goBack() {
            goMain();
        },
        editCategory(index) {
            // showAlert(this, category);
            // console.log("index=" + index + ",categories[i]=" + JSON.stringify(this.categories[index]));
            this.currentCategory = copyByJSON(this.categories[index]);
            this.currentCategory.open = this.currentCategory.open + "";
            this.editingCategory = true;
        },
        // 完成分类的编辑
        finishEdit() {
            // showAlert(this, this.currentCategory);
            this.loadingEditCategory = true;
            const url = this.curPrefixUrl + "/user/categories";
            axios.put(url, this.currentCategory).then((resp) => {
                const result = resp.data;

                if (result.code === resultConstant.success) {
                    this.editing = !this.editing;
                    // 提示
                    showSuccessMsg(this, "分类修改成功");
                    // 查询分类
                    this.getAllCategories();
                } else {
                    showErrorMsg(this, result.msg);
                }
            }).catch((error) => {
                showErrorMsg(this, error);
            }).finally(() => {
                this.loadingEditCategory = false;
                this.editingCategory = false;
            });
        },
        // 点击了删除
        deleteCategory(index) {
            let categoryId = this.categories[index].id;
            const _this = this;
            showConfirmAlert(this, msgConstant.deleteConfirm,
                function () {
                    console.log("ok:categoryId=" + categoryId);
                    // showAlert(_this, row);
                    _this.deleteCategoryById(categoryId);
                },
                function () {
                    console.log("cancel:categoryId=" + categoryId);
                    showMsg(_this, msgConstant.cancelDelete);
                });
        },
        deleteCategoryById(categoryId) {
            this.loading = true;
            const url = this.curPrefixUrl + "/user/categories/" + categoryId;
            axios.delete(url).then((resp) => {
                const result = resp.data;

                if (result.code === resultConstant.success) {
                    // 提示
                    showSuccessMsg(this, msgConstant.deleteSuccess);
                    // 查询分类
                    this.getAllCategories();
                } else {
                    showErrorMsg(this, result.msg);
                }
            }).catch((error) => {
                showErrorMsg(this, error);
            }).finally(() => {
                this.loading = false;
            });
        },
        // 取消分类的编辑
        cancelEdit() {
            this.editingCategory = false;
            this.currentCategory = {};
        },
        // 查询我的所有分类
        getAllCategories() {
            this.loading = true;
            getMyCategories(() => {
                this.loading = false;
            }).then((categoriesData) => {
                this.categories = categoriesData;
                // console.log("categories=" + JSON.stringify(this.categories));
            });
        }
    },
    created() {
        this.getAllCategories();
    }
});